package com.mangguo.tingjian.account.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.mangguo.tingjian.account.dto.MenuDto;
import com.mangguo.tingjian.account.mapper.AuthorityMapper;
import com.mangguo.tingjian.account.mapper.MenuMapper;
import com.mangguo.tingjian.account.entity.Authority;
import com.mangguo.tingjian.account.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private AuthorityMapper authorityMapper;


    /**
     * 查询自己的菜单
     * @param user
     * @return
     */
    public List<MenuDto> findOwnMenu(User user){
        if(user == null){
            return null;
        }
        //先查权限
        List<Authority> authorities = authorityMapper.selectList(new QueryWrapper<>(Authority.builder().deptId(user.getDeptId()).roleId(user.getRoleId()).build()));
        if(authorities == null && authorities.size() == 0){
            return null;
        }
        List<Long> menuIdList = authorities.stream().map(Authority::getMenuId).collect(Collectors.toList());

        List<MenuDto> menuDtoList = menuMapper.selectMenusByIds(menuIdList).stream()
                .map(menu -> MenuDto.builder()
                        .id(menu.getId())
                        .code(menu.getCode())
                        .createTime(menu.getCreateTime())
                        .level(menu.getLevel())
                        .name(menu.getName())
                        .parentId(menu.getParentId())
                        .updateTime(menu.getUpdateTime())
                        .build()).collect(Collectors.toList());
        return buildTreeMenu(menuDtoList);
    }

    /**
     * 查询所有菜单
     * @return
     */
    public List<MenuDto> findAllMenu(){
        List<MenuDto> menuDtoList = menuMapper.selectList(new QueryWrapper<>()).stream()
                .map(menu ->
                    MenuDto.builder()
                            .id(menu.getId())
                            .code(menu.getCode())
                            .createTime(menu.getCreateTime())
                            .level(menu.getLevel())
                            .name(menu.getName())
                            .parentId(menu.getParentId())
                            .updateTime(menu.getUpdateTime())
                            .build()
                ).collect(Collectors.toList());
        return buildTreeMenu(menuDtoList);
    }

    /**
     * 将菜单转换为树状结构
     * @param menuList
     * @return
     */
    private List<MenuDto> buildTreeMenu(List<MenuDto> menuList){
        return menuList.stream()
                .filter(menuDto -> Objects.equals(menuDto.getLevel(), 0))
                .map(menuDto -> findChild(menuDto, menuList))
                .collect(Collectors.toList());
    }

    private MenuDto findChild(MenuDto parentMenu, List<MenuDto> menuDtoList){
        menuDtoList.forEach(menuDto -> {
            if(Objects.equals(parentMenu.getId(), menuDto.getParentId())){
                if(parentMenu.getChildrenMenus() == null){
                    parentMenu.setChildrenMenus(Lists.newArrayList());
                }
                parentMenu.getChildrenMenus().add(findChild(menuDto, menuDtoList));
            }
        });
        return parentMenu;
    }

}
